/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web.idarea;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.idarea.IdArea;
import com.thinkgem.jeesite.modules.sys.service.idarea.IdAreaService;

/**
 * idAreaController
 * @author jin
 * @version 2018-04-16
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/idarea/idArea")
public class IdAreaController extends BaseController {

	@Autowired
	private IdAreaService idAreaService;
	
	@ModelAttribute
	public IdArea get(@RequestParam(required=false) String id) {
		IdArea entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = idAreaService.get(id);
		}
		if (entity == null){
			entity = new IdArea();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:idarea:idArea:view")
	@RequestMapping(value = {"list", ""})
	public String list(IdArea idArea, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<IdArea> page = idAreaService.findPage(new Page<IdArea>(request, response), idArea); 
		model.addAttribute("page", page);
		return "modules/sys/idarea/idAreaList";
	}

	@RequiresPermissions("sys:idarea:idArea:view")
	@RequestMapping(value = "form")
	public String form(IdArea idArea, Model model) {
		model.addAttribute("idArea", idArea);
		return "modules/sys/idarea/idAreaForm";
	}

	@RequiresPermissions("sys:idarea:idArea:edit")
	@RequestMapping(value = "save")
	public String save(IdArea idArea, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, idArea)){
			return form(idArea, model);
		}
		idAreaService.save(idArea);
		addMessage(redirectAttributes, "保存身份证归属地成功");
		return "redirect:"+Global.getAdminPath()+"/sys/idarea/idArea/?repage";
	}
	
	@RequiresPermissions("sys:idarea:idArea:edit")
	@RequestMapping(value = "delete")
	public String delete(IdArea idArea, RedirectAttributes redirectAttributes) {
		idAreaService.delete(idArea);
		addMessage(redirectAttributes, "删除身份证归属地成功");
		return "redirect:"+Global.getAdminPath()+"/sys/idarea/idArea/?repage";
	}

}